<?php
namespace app\home\controller;
use think\Controller;
use think\Db;
use think\Session;
class User extends Controller
{
    public function _initialize()
    {
        if(empty(Session::get('username')))
         $this->redirect('Login/index');   
    }

    public function index()
    {
         $data=Db::table('book')->paginate(5);
         $arr=Db::table('file')->select();
         $this->assign('arr',$arr);
         $page = $data->render();
         $this->assign('page',$page);
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function addfun()
    {
        $data=input();
        $i=Db::table('book')->where('bookname',$data['bookname'])->count();
        if($i>0)
            $this->error('该书以添加');
        else{
        $result=Db::table('book')->insert($data);
        if($result)
            $this->success('添加成功','User/index');
        else
            $this->error('添加失败','User/index');
        }
    }
    public function addfun1()
    {
        $data=input();
        $i=Db::table('book')->where('bookname',$data['bookname'])->count();
        if($i>0)
            return ['status'=>0,'message'=>'该书已添加'];
        else{
        $id=Db::table('book')->insertGetId($data);
        if($id)
        {
            $data['id']=$id;
            return ['status'=>1,'message'=>'添加成功','data'=>$data];
        }
        else
            return ['status'=>1,'message'=>'添加失败'];
        }
    }

    public function delete()
    {
        $id=input('id');
        $result=Db::table('book')->where('id',$id)->delete();
        if($result)
            $this->success('删除成功','User/index');
        else
            $this->error('删除失败','User/index');
    }

   public function delete1()
    {
        $id=input('id');
        $result=Db::table('book')->where('id',$id)->delete();
        if($result)
            return ['status'=>1,'message'=>'删除成功'];
        else
            return ['status'=>0,'message'=>'删除失败'];
    }

    public function update()
    {
        $id=input('id');
        Session::set('id',$id);
        $data=Db::table('book')->where('id',$id)->select();
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function updatefun()
    {
        $id=Session::get('id');
        $data=$_POST;
        $result=Db::table('book')->where('id',$id)->update($data);
        if($result)
            $this->success('修改成功','User/index');
        else
            $this->error('修改失败','User/update');
    }
	
	public function updatefun1()
	{
		$id=input('id');
		$value=input('value');
		$column=input('column');
		if(Db::name('book')->where('id',$id)->setField($column,$value))
			return ['status'=>1,'message'=>'修改成功'];
		else
			return ['status'=>0,'message'=>'修改失败'];
	}

    public function search()
    {
        $bookname=input('bookname');
        $sql="select * from book where bookname like '%{$bookname}%'";
        $data=Db::query($sql);
        // $value['bookname']=['like','%'.$bookname.'%'];
        // $data=Db::table('book')->where($value)->select();
        $this->assign('data',$data);
        return $this->fetch('index');
    }
    
    public function loginout()
    {
        Session::clear();
        $this->redirect('Login/index');
    }
    public function upload()
    {
     // 获取表单上传文件 例如上传了001.jpg
    $file = request()->file('filename');
    
    // 移动到框架应用根目录/public/uploads/ 目录下
    $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
    if($info){

        $data['filename']=$info->getSaveName();
        $result=Db::table('file')->insert($data);
            $this->success('上传成功');

    }else{
        // 上传失败获取错误信息
        $this->error($file->getError());
    }       
    }

}